{
 "cells": [
  {
   "cell_type": "raw",
   "id": "0aed0743",
   "metadata": {},
   "source": [
    "---\n",
    "keywords: [AzureOpenAIEmbeddings]\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3852491",
   "metadata": {},
   "source": [
    "# Azure OpenAI\n",
    "\n",
    "Let's load the Azure OpenAI Embedding class with environment variables set to indicate to use Azure endpoints."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8a6ed30d-806f-4800-b5fd-d04126be9060",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\"\n",
    "os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://<your-endpoint>.openai.azure.com/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "20179bc7-3f71-4909-be12-d38bce009b18",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import AzureOpenAIEmbeddings\n",
    "\n",
    "embeddings = AzureOpenAIEmbeddings(\n",
    "    azure_deployment=\"<your-embeddings-deployment-name>\",\n",
    "    openai_api_version=\"2023-05-15\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f8cb9dca-738b-450f-9986-5c3efd3c6eb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"this is a test document\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0fae0295-b117-4a5a-8b98-500c79306551",
   "metadata": {},
   "outputs": [],
   "source": [
    "query_result = embeddings.embed_query(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "65a01ddd-0bbf-444f-a87f-93af25ef902c",
   "metadata": {},
   "outputs": [],
   "source": [
    "doc_result = embeddings.embed_documents([text])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "45771052-68ca-4e03-9c4f-a0c7796d9442",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-0.012222584727053133,\n",
       " 0.0072103982392216145,\n",
       " -0.014818063280923775,\n",
       " -0.026444746872933557,\n",
       " -0.0034330499700826883]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "doc_result[0][:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e66ec1f2-6768-4ee5-84bf-a2d76adc20c8",
   "metadata": {},
   "source": [
    "## [Legacy] When using `openai<1`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b40f827",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set the environment variables needed for openai package to know to reach out to azure\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_TYPE\"] = \"azure\"\n",
    "os.environ[\"OPENAI_API_BASE\"] = \"https://<your-endpoint.openai.azure.com/\"\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"your AzureOpenAI key\"\n",
    "os.environ[\"OPENAI_API_VERSION\"] = \"2023-05-15\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb36d16c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import OpenAIEmbeddings\n",
    "\n",
    "embeddings = OpenAIEmbeddings(deployment=\"your-embeddings-deployment-name\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "228abcbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"This is a test document.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60dd7fad",
   "metadata": {},
   "outputs": [],
   "source": [
    "query_result = embeddings.embed_query(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83bc1a72",
   "metadata": {},
   "outputs": [],
   "source": [
    "doc_result = embeddings.embed_documents([text])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aaad49f8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "poetry-venv",
   "language": "python",
   "name": "poetry-venv"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  },
  "vscode": {
   "interpreter": {
    "hash": "7377c2ccc78bc62c2683122d48c8cd1fb85a53850a1b1fc29736ed39852c9885"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
